

#include "test2.h"


void tenacitas::log::test2::f0( std::int16_t p_repeats,
                                std::int16_t p_stop_at,
                                std::int32_t p_sleep,
                                const std::string p_prefix ) {

    text_file::ptr l_logger( text_file::create( ) );
    
    std::int16_t l_counter = 0;

    log_trace( l_logger ) << p_prefix << ", "
                          << p_repeats << ", "
                          << p_stop_at << ", "
                          << p_sleep;

    
    for ( std::int16_t l_i = 0; l_i < p_repeats; ++l_i ) {
        if ( l_counter >= p_stop_at ) {
            log_trace( l_logger ) << p_prefix << " counter = " << l_counter
                                  << ", so gone sleep";
            
            boost::this_thread::sleep(boost::posix_time::milliseconds(p_sleep));

            l_counter = 0;
            log_trace( l_logger ) << p_prefix << " woke up";
        }

        ++l_counter;

        log_trace( l_logger ) << p_prefix << ", " << l_i << " executions";        
    }
    std::cout << p_prefix << " leaving"  << std::endl;
}


void tenacitas::log::test2::run( ) {

    std::cout << "TEST2 BEGIN ============" << std::endl;    

    boost::thread_group l_thread_group;


    l_thread_group.add_thread( new boost::thread( f0, 1000, 100, 200, "one") );

    l_thread_group.add_thread( new boost::thread( f0, 3000, 300, 500, "two") );

    l_thread_group.add_thread( new boost::thread( f0, 1000, 800, 300, "three") );

    l_thread_group.add_thread( new boost::thread( f0, 200, 30, 900, "four") );

    l_thread_group.add_thread( new boost::thread( f0, 5000, 1000, 1000, "five")
        ); 

    l_thread_group.add_thread( new boost::thread( f0, 100, 1, 200, "six") );

    l_thread_group.add_thread( new boost::thread( f0, 700, 130, 300, "seven") );

    l_thread_group.add_thread( new boost::thread( f0, 2000, 1000, 100, "eight")
        );

    l_thread_group.add_thread( new boost::thread( f0, 900, 50, 1000, "nine") );

    l_thread_group.add_thread( new boost::thread( f0, 10, 2, 500, "ten") );

    l_thread_group.join_all( );

    std::cout << "TEST2 END ============" << std::endl;    

}



